import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: elin11
 * Date: Jul 28, 2008
 * Time: 9:38:55 AM
 * To change this template use File | Settings | File Templates.
 */
public class SingleSegment implements Segment{

    private static HashMap<Character, Double>aminoAcidMasses;
    //using byte to save space, probably not much but might help,
    // statusCount = -128 in case of - gap character, and -127 in case of X anyCharacter
    // -126 represents a perfectly known sequence and anything above that counts the number of ambiguous Z and B chars
    private byte statusCount;
    //Mass index creation
    public static void hashmassGenerate(String fname) throws IOException {
        aminoAcidMasses = new HashMap<Character,Double>();
        BufferedReader reader = new BufferedReader(new FileReader(fname));
        //Loop as long as there are input lines.
        String readline = null;
        while ((readline=reader.readLine()) != null) {
            aminoAcidMasses.put((Character)readline.charAt(0),Double.valueOf(readline.substring(2)));
        }
        reader.close();
    }
    private boolean setMass()throws IOException{
        totalMass = 0.0;
        statusCount = -125;
        if(sequence.isEmpty())
            return false;
        for(int i = 0;i<sequence.length();i++){
           if(aminoAcidMasses.containsKey((Character)sequence.charAt(i)) == true){
        	   totalMass += aminoAcidMasses.get((Character)sequence.charAt(i));
        	   if((sequence.charAt(i) == 'Z' || sequence.charAt(i) == 'B') &&statusCount >= -125){
        		   statusCount++;
        	   }
                   
           }else if(sequence.charAt(i) == '-'){
        	   statusCount = -127;
           }else if(sequence.charAt(i)=='X'){
        	   statusCount = -126;
           }else if(sequence.charAt(i)=='*' && i != sequence.length()-1){
        	   statusCount = -128;
           }else{
        	   throw new IOException("Sequence Character cannot be Read" + sequence.charAt(i));
           }
        }
        return true;
    }
    private String sequence;
    private Double totalMass;
    private peptideGroup pgroup;
    public peptideGroup getPgroup() {
        return pgroup;
    }

    public void setPgroup(peptideGroup pgroup) {
        this.pgroup = pgroup;
    }

    public SingleSegment(String sequence,String fname)throws IOException {
        this.sequence = sequence;
        if(aminoAcidMasses == null){
           hashmassGenerate(fname);
        }
        setMass();
    }
    public byte getStatus(){
    	return statusCount;
    }
    public String getSequence() {
        return sequence;
    }
    public Double getMass() {
        return totalMass;
    }
	@Override
	public short getBZUncertanties() {
		// TODO Auto-generated method stub
		return (short) (statusCount+125);
	}
	@Override
	public String getCallString() {
		// TODO Auto-generated method stub
		return pgroup.getProteinName();
	}
	@Override
	public int getLength() {
		// TODO Auto-generated method stub
		return 1;
	}
}
